﻿@inherits DbContextPage

<RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Center" AlignItems="AlignItems.Center" Gap="0.5rem" class="rz-p-12">
    <RadzenLabel Text="Select Value" Component="DropDownDataGridBindValue" />
    <RadzenDropDownDataGrid GridLines="Radzen.DataGridGridLines.Both" @ref=grid Data="@data" ColumnWidth="200px" TValue="IDictionary<string, object>" 
                            AllowFiltering="true" AllowSorting="true" Value="@selectedItem" Change="@OnChange"
                            TextProperty="@(PropertyAccess.GetDynamicPropertyExpression("LastName", typeof(string)))">
        <ValueTemplate>
            @string.Join(", ", columns.Where(c => c.Value == typeof(string)).Take(grid.MaxSelectedLabels).Select(c => context[c.Key]))
        </ValueTemplate>
        <Columns>
            @foreach (var column in columns)
            {
                <RadzenDropDownDataGridColumn @key=@column.Key Title="@column.Key" Type="column.Value"
                                              Property="@PropertyAccess.GetDynamicPropertyExpression(column.Key, column.Value)">
                    <Template>
                        @context[@column.Key]
                    </Template>
                </RadzenDropDownDataGridColumn>
            }
        </Columns>
    </RadzenDropDownDataGrid>
</RadzenStack>

@code {
    RadzenDropDownDataGrid<IDictionary<string, object>> grid;
    IDictionary<string, object> selectedItem;

    public IEnumerable<IDictionary<string, object>> data { get; set; }

    public IDictionary<string, Type> columns { get; set; }

    public enum EnumTest
    {
        EnumValue1,
        EnumValue2
    }

    void OnChange(object value)
    {
        selectedItem = (IDictionary<string, object>)value;
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        columns = new Dictionary<string, Type>()
        {
            { "EmployeeID", typeof(int) },
            { "MyColumn", typeof(EnumTest) },
            { "FirstName", typeof(string) },
            { "LastName", typeof(string) },
            { "HireDate", typeof(DateTime) },
        };

        foreach (var i in Enumerable.Range(0, 50))
        {
            columns.Add($"Column{i}", typeof(string));
        }

        data = Enumerable.Range(0, 100).Select(i =>
        {
            var row = new Dictionary<string, object>();

            foreach (var column in columns)
            {
                row.Add(
                    column.Key,
                    column.Value == typeof(EnumTest)
                        ? (i % 2 == 0 ? EnumTest.EnumValue1 : EnumTest.EnumValue2)
                        : column.Value == typeof(int)
                            ? i
                            : column.Value == typeof(DateTime)
                                ? DateTime.Now.AddMonths(i)
                                : $"{column.Key}{i}"
                );
            }

            return row;
        });
    }
}